package com.lmxdawn.api.im.handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.lmxdawn.api.admin.util.RedisUtil;
import com.lmxdawn.api.im.service.ImUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.jim.core.*;
import org.jim.core.exception.ImException;
import org.jim.core.packets.*;
import org.jim.core.utils.JsonKit;
import org.jim.server.JimServerAPI;
import org.jim.server.config.ImServerConfig;
import org.jim.server.listener.AbstractImGroupListener;
import org.jim.server.protocol.ProtocolManager;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 群组绑定监听器
 * @author WChao 
 * 2017年5月13日 下午10:38:36
 */
@Slf4j
public class ImDemoGroupListener extends AbstractImGroupListener {

	private  static Logger logger = LoggerFactory.getLogger(ImDemoGroupListener.class);

	@Override
	public void doAfterBind(ImChannelContext imChannelContext, Group group) throws ImException {
		JoinGroupRespBody joinGroupRespBody = JoinGroupRespBody.success();
		//回一条消息，告诉对方进群结果
		joinGroupRespBody.setGroup(group.getGroupId());
		User current = imChannelContext.getSessionContext().getImClientNode().getUser();
		logger.info("群组:{},绑定成功!{}", JsonKit.toJSONString(group), JSON.toJSONString(current));
//		ImPacket respPacket = ProtocolManager.Converter.respPacket(joinGroupRespBody, imChannelContext);
		//Jim.send(imChannelContext, respPacket);
		//发送进房间通知;
		joinGroupNotify(group, imChannelContext);
		if (!"admin".equals(group.getGroupId())
				&& !"客服".equals(group.getGroupId())
				&& !"0".equals(group.getGroupId())
		){
			ImUtil.moveUserToFirst(group.getGroupId());
		}
		var messageHelper = ((ImServerConfig) imChannelContext.getImConfig()).getMessageHelper();
		if (current != null){
			var offlineMessage = messageHelper.getGroupOfflineMessage(current.getId(),group.getGroupId());
		}
	}

	/**
	 * @param imChannelContext
	 * @param group
	 * @throws Exception
	 * @author: WChao
	 */
	@Override
	public void doAfterUnbind(ImChannelContext imChannelContext, Group group) throws ImException {
		//发退出房间通知  COMMAND_EXIT_GROUP_NOTIFY_RESP
		ExitGroupNotifyRespBody exitGroupNotifyRespBody = new ExitGroupNotifyRespBody();
		exitGroupNotifyRespBody.setGroup(group.getGroupId());
		User clientUser = imChannelContext.getSessionContext().getImClientNode().getUser();
		if(clientUser == null) {
			return;
		}
		User notifyUser = User.newBuilder().userId(clientUser.getUserId()).nick(clientUser.getNick()).build();
		exitGroupNotifyRespBody.setUser(notifyUser);

		RespBody respBody = new RespBody(Command.COMMAND_EXIT_GROUP_NOTIFY_RESP,exitGroupNotifyRespBody);
		ImPacket imPacket = new ImPacket(Command.COMMAND_EXIT_GROUP_NOTIFY_RESP, respBody.toByte());
		JimServerAPI.sendToGroup(group.getGroupId(), imPacket);
	}

	/**
	 * 发送进房间通知;
	 * @param group 群组对象
	 * @param imChannelContext
	 */
	public void joinGroupNotify(Group group, ImChannelContext imChannelContext)throws ImException{
		ImSessionContext imSessionContext = imChannelContext.getSessionContext();
		User clientUser = imSessionContext.getImClientNode().getUser();
		if (clientUser == null){
			return ;
		}
		User notifyUser = User.newBuilder()
				.userId(clientUser.getUserId())
				.nick(clientUser.getNick())
				.status(UserStatusType.ONLINE.getStatus())
				.build();
		RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
		List<Object> groupIds = redisUtil.lGet(String.format("user:%s:group", clientUser.getUserId()), 0, -1);
//		log.info("==============>{}", clientUser.getUserId());

		if(CollUtil.isNotEmpty(groupIds)) {
			for (Object groupId : groupIds) {
//				log.info("==============>{}-{}", groupId,group.getGroupId());
				if ((groupId.toString()).equals(group.getGroupId())){
					continue;
				}
//				JimServerAPI.unbindGroup(clientUser.getUserId(), groupId.toString());
			}
		}
		String groupId = group.getGroupId();
		//发进房间通知  COMMAND_JOIN_GROUP_NOTIFY_RESP
		JoinGroupNotifyRespBody joinGroupNotifyRespBody = JoinGroupNotifyRespBody.success();
		joinGroupNotifyRespBody.setGroup(groupId).setUser(notifyUser);
		JimServerAPI.sendToGroup(groupId, ProtocolManager.Converter.respPacket(joinGroupNotifyRespBody,imChannelContext));
	}

}